package com.CampusLife.controller;


import  com.CampusLife.bean.Login;
import  com.CampusLife.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import  com.CampusLife.service.ILoginService;
import  com.CampusLife.service.IUerService;
import  com.CampusLife.until.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/login")
public class LoginController {
    @Resource
    private  IUerService iUerService;
    @Resource
    private ILoginService iLoginService;

    /**
     *
      进入登录页面
     * @return
     */
    @RequestMapping("/in")
    public String index(){
        return "/home/login";
    }
    //用户验证登录
    @RequestMapping("/login_verify")
    public  String login_verify(String username,String password,HttpSession httpSession){
          //给页面传过来的密码加密
        String ps=Encrypt.MD5(password)+"wun";
          //再次加密防止密码外泄
        String  ps1=Encrypt.MD5(ps);
          User user=iUerService.login( username, ps1);
          //如果查询结果不为空，则证明该用户合法，否则该用户不合法
        if (user!=null){
            //将该用户的id保存到session
           httpSession.setAttribute("id",user.getId());
           httpSession.setAttribute("password",ps1);
           httpSession.setAttribute("username",user.getUsername());
           int n=(int)httpSession.getAttribute("id");
           String m=(String)httpSession.getAttribute("password");
           String name=(String)httpSession.getAttribute("username");
           System.out.println(n);
           System.out.println(m);
           System.out.println(name);

             return "redirect:/login/record";

          }else {
              return "login";
          }
    }
    //用户登录记录
    @RequestMapping("/record")
    public String record(HttpSession httpSession,Model model){
        DataTime dataTime=new DataTime();
        Login login=new Login();
        //获取当前登录用户名
        String name=(String)httpSession.getAttribute("username");
        System.out.println(name);
        model.addAttribute("name",name);
        login.setName(name);
        //调用datatime类中的方法，将当前时间转化成固定格式
        String data=dataTime.gettime();
        System.out.println(data);
        login.setTime(data);
        //调用方法
        iLoginService.add(login);
          return "redirect:/productType/querytype";
    }




    //退出登录

    @RequestMapping("/login_out")
    public String  login_out(HttpSession httpSession){
       httpSession.removeAttribute("id");
        return "";
    }
   //修改用户密码
    @RequestMapping("/alter_ps")
    @ResponseBody
    //页面传递过来用户密码password 修改过的密码password1  确认密码password2
    public  String alterps(String password,HttpSession httpSession,String password1,String password2)throws Exception {
        //获取session中的密码
        String pss=(String) httpSession.getAttribute("password") ;
        //
       int id= (int)httpSession.getAttribute("id");
        //给页面传过来的密码加密
        String ps=Encrypt.MD5(password)+"wun";
        //再次加密防止密码外泄
        String  ps1=Encrypt.MD5(ps);
        //页面传过来的密码通过加密后，与session存放的当前密码进行比较
        if (ps1.equals(pss)){
            if (password1.equals(password2)&&password1!=""){
                //如果相等且密码，则把新输入的密码存入
                 User user=new User();
                 user.setId(id);
                 user.setPassword(ps1);
               iUerService.UpdatePassword(user);
               //成功返回1
                return "1";
            }else {
                return "0.0";
            }

       }
      return "0";
    }
    //用户通过手机验证找回密码
    //发送验证码

    @RequestMapping("/getPhone")
    @ResponseBody
    public Object getPhone(String phone,HttpSession httpSession){
        JsonResult jsonResult = new JsonResult();
     try{
        SendSms sendSms = new SendSms();
        RandomSMS randomSMS = new RandomSMS();
        int verifyCode = randomSMS.sendsms();
        sendSms.SMSverifyCode(phone, verifyCode);
        httpSession.setAttribute("verifyCode", verifyCode);
        httpSession.setAttribute("verifyCodeCreateTime", System.currentTimeMillis());
        return 1;
       // jsonResult.setData("发送成功！");
         }
     catch (Exception e){
         jsonResult.setData("发送失败！");
         e.printStackTrace();
     }
     return jsonResult;
    }
    @RequestMapping("/SmsVerifyCode")
    public Object SmsVerifyCode(Integer verifyCode,HttpSession httpSession){
        JsonResult jsonResult = new JsonResult();
        Integer verifyCode1=(Integer) httpSession.getAttribute("verifyCode");
        try {
            if(httpSession.getAttribute("verifyCodeCreateTime") !=null && (System.currentTimeMillis()-Long.valueOf(String.valueOf(httpSession.getAttribute("verifyCodeCreateTime"))) )> 1000 * 60  ){
                httpSession.removeAttribute("verifyCode");
                httpSession.removeAttribute("verifyCodeCreateTime");
              return 0;
               // jsonResult.setData("验证码过期");
            }
            else if (verifyCode != null &&
                    verifyCode.equals(verifyCode1)){

                httpSession.removeAttribute("verifyCode");
                httpSession.removeAttribute("verifyCodeCreateTime");
             return 1;
                //jsonResult.setData("验证成功");
            }else{
                jsonResult.setData("验证失败");
            }

        }catch(Exception e){

            e.printStackTrace();
            jsonResult.setData("验证失败");
        }
           return  jsonResult;
    }
    @RequestMapping("/findAllLog")
    public  String findList(Model model){
       List<Login> list= iLoginService.findAll();
       model.addAttribute("list",list);
       return "admin/log";
    }
    @RequestMapping("/findLogByname")
    public  String findLogByname(String name,Model model){
      List<Login> list= iLoginService.byName(name);
      model.addAttribute("list",list);
      return "admin/log";
    }
    }
